// 引入封装头信息和请求域名的axios对象
import http from "../config/http";
// 引入请求的url地址
import {
  nowPlayingListUri,
  comingSoonListUri,
  moiveDetailUrl,
  cityListUrl,
  cinemaListUrl,
} from "../url/url.js";

// 请求正在热映列表数据
export const nowPlayingListData = (pageNum) => {
  http.defaults.headers.info = "";
  http.defaults.headers.authorization = "";
  return http.get(nowPlayingListUri + pageNum);
};

// 请求即将上映列表数据
export const comingSoonListData = (pageNum) => {
  return http.get(comingSoonListUri + pageNum);
};

// 请求电影详情数据
export const moiveDetailData = (filmId) => {
  // 给axios设置请求头
  http.defaults.headers.info = "info";
  http.defaults.headers.authorization = "";
  return http.get(moiveDetailUrl + filmId);
};

// 城市列表数据
export const cityListData = async () => {
  // 给axios设置请求头
  http.defaults.headers.authorization = "";
  http.defaults.headers.info = "city";
  let ret = await http.get(cityListUrl);
  console.log(ret)


  
  // 定义基本的数据
  let cities = ret.data.data.cities;
  let codeList = []; //'A','B','C'....（完整26个字母）
  let dataList = []; // 城市信息
  let indexList = []; //'A','B','C'....（经过筛选的字母）

  // for循环生成26个字母
  for (let i = 65; i <= 90; i++) {
      codeList.push(String.fromCharCode(i));
  }

  // 开始处理
  codeList.forEach((element) => {
      // 与城市信息的pinyin字段首字母进行对比，如果符合则留下大字母
      let tempArr = cities.filter(
          (item) => element.toLowerCase() == item.pinyin.substr(0, 1)
      );
      if (tempArr.length > 0) {
          indexList.push(element);
          dataList.push({
              index: element,
              data: tempArr,
          });
      }
  });
  console.log(dataList, indexList)
  // 返回数据
  return Promise.resolve([dataList, indexList]);
};

// 请求所在城市的电影院列表
export const cinemasList = (cityId) => {
  // 给axios设置请求头
  http.defaults.headers.info = "cinema";
  http.defaults.headers.authorization = "";
  return http.get(cinemaListUrl + cityId);
};